package com.maxd.service.app;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.App;
import com.maxd.model.Banner;
import com.maxd.respository.app.AppJpaRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * APP升级的实现类
 */
@Service
public class AppServiceImpl implements IAppService {
    @Autowired
    private AppJpaRepository appJpaRepository;


    @Override
    public Result findAll() {
        return ResultUtil.success(appJpaRepository.findAll());
    }

    @Override
    public Result saveBody(App app) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        app.setCreateAt(sdf.format(now));
        return ResultUtil.success(appJpaRepository.save(app));
    }


    @Override
    public Result findOne(long id) {
        return ResultUtil.success(appJpaRepository.findById(id).orElse(null));
    }

    @Override
    public Result delete(long id) {
        appJpaRepository.deleteById(id);
        return ResultUtil.success();
    }


    @Override
    @Transactional
    public Result updateBody(Banner banner) {
        App foodBanner1 = appJpaRepository.findById(banner.getId()).orElse(null);
        assert foodBanner1 != null;
        BeanUtils.copyProperties(banner, foodBanner1);
        return ResultUtil.success();
    }


}
